package net.sxnic.workflow.utils;

import net.sxnic.workflow.node.NodeManager;
import net.sxnic.workflow.pd.PdManager;
import net.sxnic.workflow.pi.PiManager;
import net.sxnic.workflow.task.TaskManager;

import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class WfManagerUtils {

	private static PdManager pdManager;

	private static NodeManager nodeManager;

	private static PiManager piManager;

	private static TaskManager taskManager;

	public static PdManager getPdManager() {
		if (pdManager == null) {
			pdManager = (PdManager) WebApplicationContextUtils
					.getRequiredWebApplicationContext(
							ContextLoader.getCurrentWebApplicationContext().getServletContext()).getBean(
							"pdManager");
		}
		return pdManager;
	}

	public static NodeManager getNodeManager() {
		if (nodeManager == null) {
			nodeManager = (NodeManager) WebApplicationContextUtils
					.getRequiredWebApplicationContext(
							ContextLoader.getCurrentWebApplicationContext().getServletContext()).getBean(
							"nodeManager");
		}
		return nodeManager;
	}

	public static PiManager getPiManager() {
		if (piManager == null) {
			piManager = (PiManager) WebApplicationContextUtils
					.getRequiredWebApplicationContext(
							ContextLoader.getCurrentWebApplicationContext().getServletContext()).getBean(
							"piManager");
		}
		return piManager;
	}

	public static TaskManager getTaskManager() {
		if (taskManager == null) {
			taskManager = (TaskManager) WebApplicationContextUtils
					.getRequiredWebApplicationContext(
							ContextLoader.getCurrentWebApplicationContext().getServletContext()).getBean(
							"taskManager");
		}
		return taskManager;
	}

	public static void setPdManager(PdManager pdManager) {
		WfManagerUtils.pdManager = pdManager;
	}

	public static void setNodeManager(NodeManager nodeManager) {
		WfManagerUtils.nodeManager = nodeManager;
	}

	public static void setPiManager(PiManager piManager) {
		WfManagerUtils.piManager = piManager;
	}

	public static void setTaskManager(TaskManager taskManager) {
		WfManagerUtils.taskManager = taskManager;
	}
}
